package main.cn.web;

import main.cn.pojo.Order;
import main.cn.service.OrderService;
import main.cn.service.impl.OrderServiceImpl;
import main.cn.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet(urlPatterns = "/client/orderServlet")
public class OrderServlet extends BaseServlet{

    private OrderService orderService = new OrderServiceImpl();


    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Order order = WebUtils.copyParamToBean(req.getParameterMap(), new Order());
        orderService.addOrder(order);
        resp.sendRedirect(req.getContextPath() + "/client/orderServlet?action=list");
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getParameter("id"));
        int cid = WebUtils.parseInt(req.getParameter("id"),0);
//        2、调用OrderService.deleteOrder();删除
        orderService.deleteOrderById(cid);
        resp.sendRedirect(req.getContextPath() + "/client/orderServlet?action=list");
    }


    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Order order = WebUtils.copyParamToBean(req.getParameterMap(), new Order());
        orderService.updateOrder(order);
        resp.sendRedirect(req.getContextPath() + "/client/orderServlet?action=list");
    }


    protected void getOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int wid = WebUtils.parseInt(req.getParameter("id"),0);
        Order order = orderService.queryOrderById(wid);
        req.setAttribute("orders",order);
        req.getRequestDispatcher("/page/edit/order_edit.jsp").forward(req,resp);
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Order> orders = orderService.queryOrders();

        request.setAttribute("orders", orders);

        request.getRequestDispatcher("/page/client/order.jsp").forward(request, response);
    }
}
